#include "tab2.h"
#include "imgui.h"
#include "mgr.h"
#include <format>
#include <ranges>
#include <vector>

extern ImVec2 btn_size;

using namespace std;

extern bool is_init;

static bool show_info_test{false};

static bool show_info_gs58{false};
static uint64 gs58{0};
static uint64 gs58_local{0};

static bool show_info_get_player{false};

static bool show_info_iter_unit{false};
static vector<shared_ptr<Unit>> units;

static bool show_info_iter_ground{false};
static vector<shared_ptr<Ground>> grounds;

static bool show_info_iter_container{false};
static vector<Container> containers;

static bool show_info_iter_container_items{false};
static vector<shared_ptr<Item>> container_items;

static bool show_info_iter_equips{false};
static vector<shared_ptr<Item>> equip_items;

static char macro_buf[256]{0};

static bool show_info_iter_buff{false};
static unordered_map<string, Buff> buffs;

static bool show_info_iter_spell{false};
static unordered_map<string, Spell> spells;

void tab2() {
  ImGui::BeginDisabled(!is_init);
  btn_info_test();

  btn_info_gs58();
  ImGui::SameLine();
  btn_info_get_player();
  ImGui::SameLine();
  btn_info_iter_unit();
  ImGui::SameLine();
  btn_info_iter_ground();

  btn_info_iter_container();
  ImGui::SameLine();
  btn_info_iter_container_items();

  btn_info_iter_equips();

  btn_action_move();
  ImGui::SameLine();
  btn_action_macro();
  ImGui::SameLine();
  ImGui::InputText("##hide_label", macro_buf, 256);

  btn_info_iter_buff();
  ImGui::SameLine();
  btn_info_iter_spell();
  ImGui::EndDisabled();

  show_info();
}

void show_info() {
  if (show_info_test) {
    // auto content = game()->get_spell_name(0x4EF9);
    // ImGui::Text("%s", content.c_str());
    // if (ImGui::Button("关闭 test 信息")) {
    //   show_info_test = false;
    // }
  }

  if (show_info_gs58) {
    if (ImGui::Button("关闭 GS 信息")) {
      show_info_gs58 = false;
    }
    ImGui::Text("gs:[58] game:  %llX", gs58);
    ImGui::Text("gs:[58] local: %llX", gs58_local);
  }

  if (show_info_get_player) {
    if (ImGui::Button("关闭 player 信息")) {
      show_info_get_player = false;
    }
    auto player = api()->player();
    ImGui::Text("player: %s[%llX] type[%X] index[%X]", player->name().c_str(),
                player->ptr(), player->type(), player->index());
    ImGui::Text("ID[%llX][%llX]", player->id1(), player->id2());
    ImGui::Text("坐标(%f, %f, %f)[%f, %f]", player->x(), player->y(),
                player->z(), player->h(), player->v());
    ImGui::Text("坐标(%X, %X, %X)[%X, %X]", r4(player->ptr() + 0x130),
                r4(player->ptr() + 0x134), r4(player->ptr() + 0x138),
                r4(player->ptr() + 0x140), r4(player->ptr() + 0x144));
  }

  if (show_info_iter_unit) {
    if (ImGui::Button("关闭 unit 遍历")) {
      show_info_iter_unit = false;
    }
    for (auto unit : units) {
      ImGui::Text("Unit obj: [%llX][%s]", unit->ptr(), unit->name().c_str());
      ImGui::SameLine();
      ImGui::Text(" type: %X", unit->type());
      ImGui::SameLine();
      ImGui::Text(" ID: [%llX][%llX]", unit->id1(), unit->id2());
    }
  }

  if (show_info_iter_ground) {
    if (ImGui::Button("关闭 ground 遍历")) {
      show_info_iter_ground = false;
    }
    for (auto ground : grounds) {
      ImGui::Text("Ground obj: [%llX][%s](%.2f, %.2f, %.2f)", ground->ptr(),
                  ground->name().c_str(), ground->x(), ground->y(),
                  ground->z());
    }
  }

  if (show_info_iter_container) {
    if (ImGui::Button("关闭背包信息")) {
      show_info_iter_container = false;
    }
    for (auto c : containers) {
      ImGui::Text("背包: [%llX] item_id=[%X]", c.ptr(), c.item_id());
      ImGui::Text("type: %X", c.type());
      ImGui::Text("index: %X", c.index());
      ImGui::Text("ID1: %llX", c.id1());
      ImGui::Text("ID2: %llX", c.id2());
      ImGui::Text("背包大小: %d 格", c.num_slots());
    }
  }

  if (show_info_iter_container_items) {
    if (ImGui::Button("关闭背包物品遍历信息")) {
      show_info_iter_container_items = false;
    }
    ImGui::Text("背包物品数量: %llu", container_items.size());
    for (auto item : container_items) {
      ImGui::Text("-----------------");
      ImGui::Text("%s [%llX] item_id[%X] id[%llX][%llX]", item->name().c_str(),
                  item->ptr(), item->item_id(), item->id1(), item->id2());
      ImGui::Text("物品类型: %s[%X] %s[%X]", item->type_name().c_str(),
                  item->type_index(), item->subtype_name().c_str(),
                  item->subtype_index());
      ImGui::Text("稀有度[%X] 等级[%d/%d] 单价[%d] 数量[%d]", item->rarity(),
                  item->min_level(), item->level(), item->price(), item->num());
    }
  }

  if (show_info_iter_equips) {
    if (ImGui::Button("关闭装备遍历信息")) {
      show_info_iter_equips = false;
    }
    for (auto item : equip_items) {
      if (item == nullptr) {
        ImGui::Text("-----------------");
        ImGui::Text("无装备");
        continue;
      }
      ImGui::Text("-----------------");
      ImGui::Text("%s [%llX] item_id[%X] id[%llX][%llX]", item->name().c_str(),
                  item->ptr(), item->item_id(), item->id1(), item->id2());
      ImGui::Text("物品类型: %s[%X] %s[%X]", item->type_name().c_str(),
                  item->type_index(), item->subtype_name().c_str(),
                  item->subtype_index());
      ImGui::Text("稀有度[%X] 等级[%d/%d] 单价[%d] 数量[%d]", item->rarity(),
                  item->min_level(), item->level(), item->price(), item->num());
    }
  }

  if (show_info_iter_buff) {
    if (ImGui::Button("关闭 buff 遍历")) {
      show_info_iter_buff = false;
    }
    string content;
    for (auto [_, buff] : buffs) {
      auto tmp = format("{}[{:X}] 持续时间[{}] 剩余时间[{}]\n", buff.name,
                        buff.buff_id, buff.duration,
                        buff.expiration - game()->get_time());
      content += tmp;
    }
    ImGui::InputTextMultiline("##buff", content.data(), content.capacity() + 1,
                              ImVec2(-1, -1), ImGuiInputTextFlags_ReadOnly);
  }

  if (show_info_iter_spell) {
    if (ImGui::Button("关闭 spell 遍历")) {
      show_info_iter_spell = false;
    }
    string content;
    for (auto [_, spell] : spells) {
      auto tmp = format("{}[{:X}]\n", spell.name, spell.spell_id);
      content += tmp;
    }
    ImGui::InputTextMultiline("##buff", content.data(), content.capacity() + 1,
                              ImVec2(-1, -1), ImGuiInputTextFlags_ReadOnly);
  }
}

void btn_info_test() {
  if (ImGui::Button("test", btn_size)) {
    // show_info_test = true;
  }
}

void btn_info_gs58() {
  if (ImGui::Button("获取 GS:[58]")) {
    if (is_init) {
      gs58 = game()->core_base();
    } else {
      gs58 = game()->get_gs58();
    }
    gs58_local = game()->get_gs58_local();
    show_info_gs58 = true;
  }
}

void btn_info_get_player() {
  if (ImGui::Button("获取 player 信息")) {
    show_info_get_player = true;
  }
}

void btn_info_iter_unit() {
  if (ImGui::Button("Unit 遍历")) {
    units = api()->units();
    show_info_iter_unit = true;
  }
}

void btn_info_iter_ground() {
  if (ImGui::Button("Ground 遍历")) {
    grounds = api()->grounds();
    show_info_iter_ground = true;
  }
}

void btn_info_iter_container() {
  if (ImGui::Button("背包遍历")) {
    containers.clear();
    for (auto i : views::iota(0, 4)) {
      auto container = api()->get_container(i);
      if (container.has_value()) {
        containers.push_back(container.value());
      }
    }
    show_info_iter_container = true;
  }
}

void btn_info_iter_container_items() {
  if (ImGui::Button("背包物品类型")) {
    container_items = api()->container_items();
    for (auto item : container_items) {
      auto type = item->type_index();
      auto subtype = item->subtype_index();
      dbgprintu8("codergege [%s][%03d] %X-%s %X-%s %s[%llX]\n", __FUNCTION__,
                 __LINE__, item->type_index(), item->type_name().c_str(),
                 item->subtype_index(),
                 game()->get_item_subtype(type, subtype).c_str(),
                 item->name().c_str(), item->ptr());
    }
  }
  ImGui::SameLine();
  if (ImGui::Button("背包物品遍历")) {
    container_items = api()->container_items();
    show_info_iter_container_items = true;
  }
}

void btn_info_iter_equips() {
  if (ImGui::Button("装备遍历")) {
    equip_items = api()->equip_items();
    show_info_iter_equips = true;
  }
}

void btn_action_move() {
  if (ImGui::Button("move")) {
    auto p = api()->player();
    api()->go(p->x() + 10, p->y() + 10, p->z());
  }
}

void btn_action_macro() {
  if (ImGui::Button("宏(use/cast/run)")) {
    game()->macro(macro_buf);
  }
}

void btn_info_iter_buff() {
  if (ImGui::Button("buff 遍历")) {
    buffs.clear();
    // TODO: target 作为参数
    buffs = api()->buffs();
    show_info_iter_buff = true;
  }
}

void btn_info_iter_spell() {
  if (ImGui::Button("spell 遍历")) {
    spells = api()->spells();
    show_info_iter_spell = true;
  }
}
